// main.cpp
// Sample decompressor/compressor program
// Written by Stephen Wong

/** 
 * This file contains a sample program that utilizes the JPEG compression and
 * decompression tools, as well as writing to BMP format.  See CImg.h for
 * proper usage.
 *
 * LIBJAVA Open Source Copyright (C) 1991-1998, Thomas G. Lane.
 * A member of the Independent JPEG Group.
 */

// include JPEG compressor/decompressor tools
#include "CImg.h"
#include <iostream>
using namespace std;

// definitions
#define QUALITY 80		// JPEG compression quality factor (in percent)

int main()
{
	char infile[80] = "whistler.jpg";	// JPEG input file
	char outfile[80] = "output.jpg";	// JPEG output file
	char outfile2[80] = "output.bmp";	// BMP output file

	CImg * image = new CImg(MODE_RGB);	// Create an empty CImg object
	
	cout << "Reading input file " << infile << "... ";
	// Sample decompression code ==============================================
	// Since the CImg object created is MODE_RGB, the file will be read with RGB.
	if (!image->read_JPEG_file(infile))
	{
		cout << "Error: Image not found." << endl;
		return EXIT_FAILURE;
	}
	//=========================================================================
	cout << "complete!" << endl;
	
	// Copy an instance of the first image
	CImg * image2 = new CImg(*image);
	// Set mode to grayscale, thus desaturating the image
	image2->setMode(MODE_GRAYSCALE);

	cout << "Writing JPEG image file " << outfile << "... ";
	// Sample compression code ================================================
	// Since the CImg object is MODE_GRAYSCALE, the output JPEG will be in
	// grayscale.
	if (!image2->write_JPEG_file(outfile, QUALITY))
	{
		cout << "Error: Image was not written due to access errors" << endl;
		return EXIT_FAILURE;
	}
	//=========================================================================
	cout << "complete!" << endl;
	
	cout << "Writing BMP image file " << outfile2 << "... ";
	if (image->write_BMP_file(outfile2) != WRITE_BMP_SUCCESS)
	{
		cout << "Error: Image was not written due to write errors" << endl;
	}
	cout << "complete!" << endl;

	// Delete created CImg objects
	delete image2;
	delete image;

	return EXIT_SUCCESS;
}